package com.huonan.jb.treads.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


/**
 * @Description thenApply 使用，一个线程执行完成，紧接着完成操作
 * @Param
 * @return
 **/
public class ThenApplyTest1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 3; i++) {
            int finalI = i;
            CompletableFuture<String> welcomeText = CompletableFuture.supplyAsync(() -> {
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new IllegalStateException(e);
                }
                return "执行第" + finalI + "步 ";
            }, executor).thenApply(name -> name + "执行第2步 ").thenApply(name -> name + "执行第3步");

            System.out.println(welcomeText.get());
        }
        executor.shutdown();
        if (executor.isTerminated()) {
            Thread.yield();
        }
    }

    public  static  String nextStep(){


        return "";
    }
}